package com.dinstar.dinlink.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.dinstar.dinlink.R;
import com.dinstar.dinlink.utils.PermissionsUtils;

import core.library.com.widget.toupload.constant.UiType;
import core.library.com.widget.toupload.model.UiConfig;
import core.library.com.widget.toupload.model.UpdateConfig;
import core.library.com.widget.toupload.update.UpdateAppUtils;
import kotlin.Unit;
import kotlin.jvm.functions.Function0;

public class LoginActivity extends AppCompatActivity {
    private static final String TAG = "--LoginActivity--";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "started login activity...");
        setContentView(R.layout.activity_login);

        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_IMMERSIVE);


//        if (!Settings.canDrawOverlays(this)) {
//            Intent intent =  new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:$packageName"));
//            startActivityForResult(intent, REQUEST_OVERLAY_PERMISSION);
//        }

        TextView ipView = findViewById(R.id.sip_ip);
        TextView portView = findViewById(R.id.sip_port);
        TextView accountView = findViewById(R.id.sip_account);
        TextView passwordView = findViewById(R.id.sip_password);
        Button loginBtn = findViewById(R.id.login_btn);

        String[] permissions = {
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.CAMERA,
                Manifest.permission.RECORD_AUDIO,
                Manifest.permission.POST_NOTIFICATIONS,
        };

        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                // 定义描述和标题
                String desc = "我们需要存储来对应用进行更新";
                String title = "权限请求";
                // 定义回调函数
                Function0<Unit> onGranted = new Function0<Unit>() {
                    @Override
                    public Unit invoke() {
                        String host = ipView.getText().toString();
                        String port = portView.getText().toString();
                        String sipExtension = accountView.getText().toString();
                        String passwd = passwordView.getText().toString();
                        Log.i(TAG, String.format("ip --> %s, port --> %s, sip account --> %s, sip passwd --> %s", host, port, sipExtension, passwd));
                        Intent i = new Intent(LoginActivity.this, MainActivity.class);
                        i.putExtra("mode", "linphone");
                        i.putExtra("ip", host);
                        i.putExtra("port", port);
                        i.putExtra("account", sipExtension);
                        i.putExtra("passwd", passwd);
                        startActivity(i);
                        return Unit.INSTANCE;
                    }
                };

                Function0<Unit> onDenied = new Function0<Unit>() {
                    @Override
                    public Unit invoke() {
                        Toast.makeText(LoginActivity.this,"权限被拒绝",Toast.LENGTH_LONG).show();
                        return Unit.INSTANCE;
                    }
                };

                Function0<Unit> onDeniedWithoutRequest = new Function0<Unit>() {
                    @Override
                    public Unit invoke() {
//                        System.out.println("用户拒绝了权限请求");
//                        Toast.makeText(LoginActivity.this,"用户拒绝了权限请求",Toast.LENGTH_LONG).show();
                        String host = ipView.getText().toString();
                        String port = portView.getText().toString();
                        String sipExtension = accountView.getText().toString();
                        String passwd = passwordView.getText().toString();
                        Log.i(TAG, String.format("ip --> %s, port --> %s, sip account --> %s, sip passwd --> %s", host, port, sipExtension, passwd));
                        Intent i = new Intent(LoginActivity.this, MainActivity.class);
                        i.putExtra("mode", "linphone");
                        i.putExtra("ip", host);
                        i.putExtra("port", port);
                        i.putExtra("account", sipExtension);
                        i.putExtra("passwd", passwd);
                        startActivity(i);
                        return Unit.INSTANCE;
                    }
                };
                PermissionsUtils.INSTANCE.handleRequestPermissions(LoginActivity.this,permissions , desc, title, onGranted, onDenied, onDeniedWithoutRequest);
            }
        });
    }
}
